package org.dromara.commission.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.commission.domain.bo.LzshNetWorkLatitudeBo;
import org.dromara.commission.domain.vo.LzshAgentLatitudeImportVo;
import org.dromara.commission.domain.vo.LzshNetWorkImportVo;
import org.dromara.commission.domain.vo.LzshNetWorkLatitudeVo;
import org.dromara.commission.listener.LzshNetWorkImportListener;
import org.dromara.commission.service.ILzshNetWorkLatitudeService;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.core.ExcelResult;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * 网点纬度
 *
 * @author sxl
 * @date 2024-07-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/commission/networkLatitude")
public class LzshNetworkLatitudeController extends BaseController {

    private final ILzshNetWorkLatitudeService lzshNetworkLatitudeService;
    /**
     * 查询网点纬度列表
     */
    @SaCheckPermission("commission:networkLatitude:list")
    @GetMapping("/list")
    public TableDataInfo<LzshNetWorkLatitudeVo> list(LzshNetWorkLatitudeBo bo, PageQuery pageQuery) {
//         return lzshNetworkLatitudeService.queryPageListByRedis(pageQuery);
        return lzshNetworkLatitudeService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出网点纬度列表
     */
    @SaCheckPermission("commission:networkLatitude:export")
    @Log(title = "网点纬度", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(LzshNetWorkLatitudeBo bo, HttpServletResponse response) {
        List<LzshNetWorkLatitudeVo> list = lzshNetworkLatitudeService.queryList(bo);
        ExcelUtil.exportExcel(list, "网点纬度", LzshNetWorkLatitudeVo.class, response);
    }

    /**
     * 获取网点纬度详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("commission:networkLatitude:query")
    @GetMapping("/{id}")
    public R<LzshNetWorkLatitudeVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(lzshNetworkLatitudeService.queryById(id));
    }

    /**
     * 新增网点纬度
     */
    @SaCheckPermission("commission:networkLatitude:add")
    @Log(title = "网点纬度", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody LzshNetWorkLatitudeBo bo) {
        return toAjax(lzshNetworkLatitudeService.insertByBo(bo));
    }

    /**
     * 修改网点纬度
     */
    @SaCheckPermission("commission:networkLatitude:edit")
    @Log(title = "网点纬度", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody LzshNetWorkLatitudeBo bo) {
        return toAjax(lzshNetworkLatitudeService.updateByBo(bo));
    }

    /**
     * 删除网点纬度
     *
     * @param ids 主键串
     */
    @SaCheckPermission("commission:networkLatitude:remove")
    @Log(title = "网点纬度", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(lzshNetworkLatitudeService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 获取网点纬度导入模板
     * @param response
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "网点维度导出模板", LzshNetWorkImportVo.class, response);
    }
    /**
     * 导入网点文件操作
     * @param file 网点文件
     */
    @SaCheckPermission("commission:networkLatitude:import")
    @Log(title = "网点纬度", businessType = BusinessType.IMPORT)
    @PostMapping(value = "/importNetWorkData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importNetWorkData(@RequestParam("file") MultipartFile file,boolean updateSupport) throws Exception {
        ExcelResult<LzshNetWorkImportVo> result = ExcelUtil.importExcel(file.getInputStream(), LzshNetWorkImportVo.class, new LzshNetWorkImportListener(updateSupport));
        return R.ok(result.getAnalysis());
    }
}
